package com.zhijiang.hospital.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhijiang.hospital.entity.dto.PublicDataDto;
import com.zhijiang.hospital.mapper.PublicDataDtoMapper;
import com.zhijiang.hospital.service.PublicDataDtoService;
import com.zhijiang.util.BaseQuery;
import com.zhijiang.util.NumberUtils;
import com.zhijiang.util.PageList;
import com.zhijiang.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 75936
 * @version 1.0
 * @project zhijiang_hospital_data
 * @description
 * @date 2023-02-20 16:44:35
 */
@Service
public class PublicDataDtoServiceImpl implements PublicDataDtoService {
    @Autowired
    PublicDataDtoMapper publicDataDtoMapper;

    @Override
    public PageList findPublicDataDto(BaseQuery baseQuery) {
        PageHelper.startPage(baseQuery.getPage(), baseQuery.getPageSize());
        List<PublicDataDto> publicDataDto = publicDataDtoMapper.findPublicDataDto(baseQuery.getStartTime(), baseQuery.getEndTime());
        PageInfo<PublicDataDto> pageInfo = new PageInfo<>(publicDataDto);
        Integer pages = NumberUtils.totalPages(pageInfo.getTotal(), baseQuery.getPageSize());
        return new PageList(pageInfo.getTotal(), pageInfo.getList(), baseQuery.getPage(), baseQuery.getPageSize(), pages);
    }

    @Override
    public List getDataYear() {
        DateTime dateTime = DateUtil.beginOfYear(new Date());
        DateTime dateTime1 = DateUtil.endOfYear(new Date());
        String startTime = Convert.toStr(dateTime);
        String endTime = Convert.toStr(dateTime1);
        List<PublicDataDto> publicDataDto = publicDataDtoMapper.findPublicDataDto(startTime, endTime);
        return publicDataDto;
    }

    @Override
    public List getLastYearData() {
        Date nextYearPreDay = TimeUtil.getNextYearPreDay();
        DateTime dateTime3 = DateUtil.beginOfYear(nextYearPreDay);
        DateTime dateTime4 = DateUtil.endOfYear(nextYearPreDay);
        String startTime = TimeUtil.format(dateTime3);
        String endTime = TimeUtil.format(dateTime4);
        List<PublicDataDto> publicDataDto = publicDataDtoMapper.findPublicDataDto(startTime, endTime);
        return publicDataDto;
    }
}
